package com.emperor.customer.app;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

import com.emperor.cashier.app.CashierAppCtrl;
import com.emperor.core.app.App;
import com.emperor.core.app.AppBase;
import com.emperor.core.app.AppConstant;
import com.emperor.customer.logic.Customer;
import com.emperor.customer.logic.CustomerModel;
import com.emperor.receipt.logic.Receipt;
import com.emperor.receipt.logic.ReceiptModel;
import com.emperor.util.DateFormatter;
import com.emperor.util.HibernateUtil;
import com.swtdesigner.SWTResourceManager;

public class CustomerAppNew extends AppBase implements App {

	private org.eclipse.swt.widgets.List list_1;
	private Button pickDateButton_2;
	private Button pickDateButton_1;
	private Button pickDateButton;
	private Text text_9;
	private Text text_8;
	private Text text_7;
	private Text text_6;
	private Text text_5;
	private Text text_4;
	private Text text_3;
	private Text text_2;
	private Text text_1;
	private Text text;
	private Combo combo_3;
	private Text text_14;
	private Combo combo_2;
	private Combo combo;
	private Text text_13;
	private Text text_12;
	private List list;
	private Text text_11;
	protected Shell shell;
	public Logger log = Logger.getLogger(CustomerAppNew.class);
	public SimpleDateFormat sdf = new SimpleDateFormat(DateFormatter.YYYYMMDD);
	protected String code;
	protected Integer codeid;

	public CustomerAppNew() {
		super();
	}

	public CustomerAppNew(Map<String, Object> params) {
		super();
		this.params = params;
	}

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			CustomerAppNew window = new CustomerAppNew();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setBackgroundImage(SWTResourceManager.getImage(
				CustomerAppNew.class, "/img/main.jpg"));
		shell.setImage(SWTResourceManager.getImage(CustomerAppNew.class,
				"/img/iMac OSX.png"));
		shell.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		shell.setSize(1280, 800);
		shell.setText("New Customer");

		initScreen(shell, getParams());

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(10, 50, 859, 592);

		text = new Text(composite, SWT.BORDER);
		text.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		text.setToolTipText("cust_name");
		text.setBounds(141, 36, 340, 30);

		final Label txtLabel = new Label(composite, SWT.NONE);
		txtLabel.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel.setText("Name");
		txtLabel.setBounds(10, 39, 45, 20);

		final Label txtLabel_1 = new Label(composite, SWT.NONE);
		txtLabel_1.setBounds(10, 111, 85, 20);
		txtLabel_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_1.setText("IC/Passport");

		final Label txtLabel_2 = new Label(composite, SWT.NONE);
		txtLabel_2.setBounds(10, 147, 45, 20);
		txtLabel_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_2.setText("Phone");

		final Label txtLabel_3 = new Label(composite, SWT.NONE);
		txtLabel_3.setBounds(10, 253, 90, 20);
		txtLabel_3.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_3.setText("Date of Birth");

		final Label txtLabel_4 = new Label(composite, SWT.NONE);
		txtLabel_4.setBounds(10, 366, 60, 20);
		txtLabel_4.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_4.setText("Address");

		final Label txtLabel_5 = new Label(composite, SWT.NONE);
		txtLabel_5.setBounds(10, 292, 70, 20);
		txtLabel_5.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_5.setText("Join Date");

		text_1 = new Text(composite, SWT.BORDER);
		text_1.setEditable(false);
		text_1.setBounds(141, 72, 259, 30);
		text_1.setToolTipText("cust_code");
		text_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_2 = new Text(composite, SWT.BORDER);
		text_2.setBounds(141, 108, 259, 30);
		text_2.setToolTipText("cust_ic");
		text_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_3 = new Text(composite, SWT.BORDER);
		text_3.setBounds(141, 144, 259, 30);
		text_3.setToolTipText("cust_phone");
		text_3.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_4 = new Text(composite, SWT.BORDER);
		text_4.setBounds(621, 115, 228, 30);
		text_4.setToolTipText("cust_email");
		text_4.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_5 = new Text(composite, SWT.BORDER);
		text_5.setBounds(141, 250, 165, 30);
		text_5.setToolTipText("cust_birthdate");
		text_5.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_6 = new Text(composite, SWT.BORDER);
		text_6.setBounds(141, 289, 165, 30);
		text_6.setToolTipText("cust_joindate");
		text_6.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_7 = new Text(composite, SWT.BORDER);
		text_7.setForeground(SWTResourceManager.getColor(255, 0, 0));
		text_7.setBounds(141, 325, 165, 30);
		text_7.setToolTipText("cust_expirydate");
		text_7.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		text_7.setVisible(false);

		text_6.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent arg0) {
				if (text_6.getText().length() > 0) {
					try {
						Date expiryDate = sdf.parse(text_6.getText());
						Calendar expiryCalendar = Calendar.getInstance();
						expiryCalendar.setTime(expiryDate);
						expiryCalendar.add(Calendar.YEAR, 1);
						text_7.setText(sdf.format(expiryCalendar.getTime()));
					} catch (Exception e) {
						log.error("Unable to set default expiry date.", e);
					}
				}
			}
		});

		final Label txtLabel_6 = new Label(composite, SWT.NONE);
		txtLabel_6.setBounds(10, 75, 45, 20);
		txtLabel_6.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_6.setText("Code");

		final Label txtLabel_7 = new Label(composite, SWT.NONE);
		txtLabel_7.setBounds(501, 118, 45, 20);
		txtLabel_7.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_7.setText("Email");

		text_8 = new Text(composite, SWT.BORDER);
		text_8.setVisible(false);
		text_8.setBounds(621, 303, 165, 30);
		text_8.setToolTipText("cust_discount");
		text_8.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		final Label txtLabel_8 = new Label(composite, SWT.NONE);
		txtLabel_8.setVisible(false);
		txtLabel_8.setBounds(501, 306, 65, 20);
		txtLabel_8.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_8.setText("Discount");

		final Label txtLabel_9 = new Label(composite, SWT.NONE);
		txtLabel_9.setVisible(false);
		txtLabel_9.setForeground(SWTResourceManager.getColor(255, 0, 0));
		txtLabel_9.setBounds(10, 328, 85, 20);
		txtLabel_9.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_9.setText("Expiry Date");

		text_9 = new Text(composite, SWT.WRAP | SWT.V_SCROLL
				| SWT.MULTI | SWT.BORDER);
		text_9.setBounds(141, 363, 340, 95);
		text_9.setToolTipText("cust_address");
		text_9.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		pickDateButton = new Button(composite, SWT.NONE);
		pickDateButton.setImage(SWTResourceManager.getImage(
				CustomerAppNew.class, "/img/Calendar.png"));
		pickDateButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				pickDate(getShell(), text_5);
			}
		});
		pickDateButton.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		pickDateButton.setBounds(312, 248, 30, 30);

		pickDateButton_1 = new Button(composite, SWT.NONE);
		pickDateButton_1.setImage(SWTResourceManager.getImage(
				CustomerAppNew.class, "/img/Calendar.png"));
		pickDateButton_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				pickDate(getShell(), text_6);
			}
		});
		pickDateButton_1.setBounds(312, 287, 30, 30);
		pickDateButton_1.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));

		pickDateButton_2 = new Button(composite, SWT.NONE);
		pickDateButton_2.setVisible(false);
		pickDateButton_2.setImage(SWTResourceManager.getImage(
				CustomerAppNew.class, "/img/Calendar.png"));
		pickDateButton_2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				pickDate(getShell(), text_7);
			}
		});
		pickDateButton_2.setBounds(312, 323, 30, 30);
		pickDateButton_2.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));

		final Label txtLabel_11 = new Label(composite, SWT.NONE);
		txtLabel_11.setVisible(false);
		txtLabel_11.setBounds(501, 265, 100, 20);
		txtLabel_11.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_11.setText("Receipt Code");

		text_11 = new Text(composite, SWT.BORDER);
		text_11.setVisible(false);
		text_11.setBounds(621, 262, 228, 30);
		text_11.setToolTipText("cust_rcpcode");
		text_11.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		final Button saveButton = new Button(shell, SWT.NONE);
		saveButton.setImage(SWTResourceManager.getImage(CustomerAppNew.class,
				"/img/Save.png"));
		saveButton.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		saveButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				try {
					Customer customer = new CustomerModel();

					customer = (Customer) setModel(composite, "cust", customer);
					log.debug(customer.getClass().getSimpleName() + " : "
							+ customer.getValues().toString());

					List<String> errors = validateForm(customer);
					if (errors.isEmpty()) {
						customer = (Customer) preAdd(customer, getParams());
						customer.setStatus(AppConstant.STATUS_ACTIVE);
						customer.setCode(getCode());
						customer.setCodeid(getCodeid());

						int rowCount = doAdd(customer);
						if (rowCount == 1) {
							message(getShell(), "Save Success.",
									"Customer added successfully.");

							getParams().put(AppConstant.APPCTRL,
									CustomerAppCtrl.CUST_CTRL);
							getParams().put(AppConstant.ACTION,
									CustomerAppCtrl.CUST_ENQ);
							getShell().close();
						} else {
							message(getShell(), "Save Error.",
									"Unable to add customer.", SWT.ICON_ERROR
											| SWT.OK);
						}
					} else {
						message(getShell(), "Save Error.", errors,
								SWT.ICON_ERROR | SWT.OK);
					}
				} catch (Exception ex) {
					ex.printStackTrace();
					message(getShell(), "Save Error.", ex.getMessage(),
							SWT.ICON_ERROR | SWT.OK);
					log.error(ex.getMessage(), ex);
				}
			}
		});
		saveButton.setText("Save");
		saveButton.setBounds(148, 648, 75, 30);

		final Button resetButton = new Button(shell, SWT.NONE);
		resetButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				getShell().close();
			}
		});
		resetButton.setImage(SWTResourceManager.getImage(CustomerAppNew.class,
				"/img/Refresh.png"));
		resetButton.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		resetButton.setText("Reset");
		resetButton.setBounds(229, 648, 80, 30);

		list_1 = new org.eclipse.swt.widgets.List(
				composite, SWT.BORDER);
		list_1.setVisible(false);
		list_1.setToolTipText("cust_joinmethod");
		list_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		list_1.setBounds(621, 151, 228, 78);
		list_1.add(Customer.JOIN_FOCP);
		list_1.add(Customer.JOIN_FOCE);
		list_1.add(Customer.JOIN_RM10);

		final Label txtLabel_11_1 = new Label(composite, SWT.NONE);
		txtLabel_11_1.setVisible(false);
		txtLabel_11_1.setBounds(501, 154, 100, 20);
		txtLabel_11_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_11_1.setText("Join Method");

		final Label txtLabel_10 = new Label(composite, SWT.NONE);
		txtLabel_10.setBounds(501, 82, 55, 20);
		txtLabel_10.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_10.setText("Gender");

		text_12 = new Text(composite, SWT.BORDER);
		text_12.setBounds(141, 180, 259, 30);
		text_12.setToolTipText("cust_ophone");
		text_12.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_13 = new Text(composite, SWT.BORDER);
		text_13.setBounds(141, 214, 259, 30);
		text_13.setToolTipText("cust_hphone");
		text_13.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		final Label txtLabel_2_1 = new Label(composite, SWT.NONE);
		txtLabel_2_1.setBounds(10, 186, 95, 20);
		txtLabel_2_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_2_1.setText("Office Phone");

		final Label txtLabel_2_2 = new Label(composite, SWT.NONE);
		txtLabel_2_2.setBounds(10, 219, 90, 20);
		txtLabel_2_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_2_2.setText("Hand Phone");

		combo = new Combo(composite, SWT.NONE);
		combo.setToolTipText("cust_state");
		combo.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		combo.setBounds(141, 464, 340, 28);
		combo.add(Customer.STATE_0);
		combo.add(Customer.STATE_1);
		combo.add(Customer.STATE_2);
		combo.add(Customer.STATE_3);
		combo.add(Customer.STATE_4);
		combo.add(Customer.STATE_5);
		combo.add(Customer.STATE_6);
		combo.add(Customer.STATE_7);
		combo.add(Customer.STATE_8);
		combo.add(Customer.STATE_9);
		combo.add(Customer.STATE_10);
		combo.add(Customer.STATE_11);
		combo.add(Customer.STATE_12);
		combo.add(Customer.STATE_13);
		combo.add(Customer.STATE_14);
		combo.select(0);

		combo_2 = new Combo(composite, SWT.NONE);
		combo_2.setBounds(141, 534, 259, 28);
		combo_2.setToolTipText("cust_country");
		combo_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		combo_2.add(Customer.COUNTRY_0);
		combo_2.add(Customer.COUNTRY_1);
		combo_2.add(Customer.COUNTRY_2);
		combo_2.select(0);
		
		text_14 = new Text(composite, SWT.BORDER);
		text_14.setBounds(141, 498, 165, 30);
		text_14.setToolTipText("cust_postcode");
		text_14.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		final Label txtLabel_12 = new Label(composite, SWT.NONE);
		txtLabel_12.setBounds(10, 467, 45, 20);
		txtLabel_12.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_12.setText("State");

		final Label txtLabel_13 = new Label(composite, SWT.NONE);
		txtLabel_13.setBounds(10, 501, 70, 20);
		txtLabel_13.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_13.setText("Postcode");

		final Label txtLabel_14 = new Label(composite, SWT.NONE);
		txtLabel_14.setBounds(10, 537, 55, 20);
		txtLabel_14.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_14.setText("Country");

		combo_3 = new Combo(composite, SWT.NONE);
		combo_3.setBounds(621, 82, 228, 28);
		combo_3.setToolTipText("cust_gender");
		combo_3.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		combo_3.add(Customer.FEMALE);
		combo_3.add(Customer.MALE);
		combo_3.select(0);
		
		//
		buildStandardMenu(getShell(), getParams(), CustomerAppCtrl.CUST_CTRL,
				CustomerAppCtrl.CUST_ENQ);
		populateCustomerCode(text_1);
		
				final Label messageLabel = new Label(composite, SWT.NONE);
				messageLabel.setBounds(10, 10, 859, 20);
				messageLabel.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
				messageLabel
						.setText("Please fill in the New Customer Form below and click Save to add the New Customer");
		composite.setTabList(new Control[] {text, text_1, text_2, text_3, text_12, text_13, text_5, text_6, text_7, text_9, combo, text_14, combo_2, combo_3, text_4, list_1, text_11, text_8, pickDateButton, pickDateButton_1, pickDateButton_2});

	}

	public Shell getShell() {
		return shell;
	}

	public void setShell(Shell shell) {
		this.shell = shell;
	}

	public static List<String> validateForm(Customer customer) throws Exception {
		List<String> errors = new ArrayList<String>();

		if (StringUtils.isEmpty(customer.getName())) {
			errors.add("Customer name cannot be empty.");
		}

		if (StringUtils.isEmpty(customer.getCode())) {
			errors.add("Customer code cannot be empty.");
		}

		if (StringUtils.isEmpty(customer.getEmail())) {
			errors.add("Customer email cannot be empty.");
		}

		if (StringUtils.isEmpty(customer.getAddress())) {
			errors.add("Customer address cannot be empty.");
		}

		if (StringUtils.isEmpty(customer.getIc())) {
			errors.add("Customer IC cannot be empty.");
		}

		if (StringUtils.isEmpty(customer.getPhone())) {
			errors.add("Customer phone cannot be empty.");
		}

		if (customer.getBirthdate() == null) {
			errors.add("Customer birthdate cannot be empty.");
		}		

		if (customer.getJoindate() == null) {
			errors.add("Customer join date cannot be empty.");
		}
		
		if (StringUtils.isEmpty(customer.getGender())) {
			errors.add("Customer gender cannot be empty.");
		}
		
		if (StringUtils.isEmpty(customer.getState())) {
			errors.add("Customer state cannot be empty.");
		}
		
		if (StringUtils.isEmpty(customer.getPostcode())) {
			errors.add("Customer postcode cannot be empty.");
		}
		
		if (StringUtils.isEmpty(customer.getCountry())) {
			errors.add("Customer country cannot be empty.");
		}

		List<Customer> customers = new ArrayList<Customer>();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Criteria cris = session.createCriteria(CustomerModel.class).add(
				Expression.eq(Customer.FLD_NAME, customer.getName()));
		if (customer.getId() != null) {
			cris.add(Expression.ne(Customer.FLD_ID, customer.getId()));
		}
		customers = cris.list();

		if (customers != null && !customers.isEmpty()) {
			errors.add("Duplicated customer name. Please use another name.");
		}

		return errors;
	}

	private int doAdd(Customer customer) throws Exception {
		int rowCount = 0;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

		try {
			session.beginTransaction();

			session.save(customer);
			rowCount++;
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			log.error(e.getMessage(), e);
			throw e;
		}
		return rowCount;
	}

	private String generateCustomerCode(Integer codeid) {
		StringBuffer sb = new StringBuffer();
		try {
			sb.append("CELL");
			if (codeid != null) {
				sb.append(codeid.toString());
			}
		} catch (Exception e) {
			log.error("Unable to generate customer code.", e);
		}
		return sb.toString();
	}

	private void populateCustomerCode(Text label) {
		try {
			Integer codeid = new Integer(Customer.START_CODEID);
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			List<Customer> customers = session.createCriteria(
					CustomerModel.class).addOrder(Order.desc(Customer.FLD_ID))
					.setMaxResults(1).list();

			if (customers != null && !customers.isEmpty()) {
				Customer customer = customers.get(0);
				codeid = customer.getCodeid().intValue() + 1;
			}
			setCodeid(codeid);
			setCode(generateCustomerCode(getCodeid()));
			label.setText(getCode());
		} catch (Exception e) {
			log.error("Unable to populate customer code label.", e);
		}
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		if (code != null) {
			if (code instanceof String) {
				this.code = (String) code;
			} else {
				try {
					this.code = code.toString();
				} catch (Exception e) {
				}
			}
		}
	}

	public Integer getCodeid() {
		return codeid;
	}

	public void setCodeid(Object codeid) {
		if (codeid != null) {
			if (codeid instanceof Integer) {
				this.codeid = (Integer) codeid;
			} else {
				try {
					this.codeid = new Integer(codeid.toString());
				} catch (Exception e) {
				}
			}
		}
	}
	
}
